package com.lizard.schedule.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.lizard.schedule.R;
import com.lizard.schedule.net.http.request.bean.ScheduleSync.ScheduleSyncItem;
import com.lizard.schedule.ui.view.ItemView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyiqian on 16/1/1.
 */
public class RepeatListAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;

    private List<ItemContent> itemContents = new ArrayList<ItemContent>();

    public RepeatListAdapter(Context context){
        layoutInflater =  LayoutInflater.from(context);
    }

    public void setData(List<ItemContent> itemContents){
        this.itemContents.clear();
        this.itemContents.addAll(itemContents);
        notifyDataSetChanged();
    }

    public List<ItemContent> getData(){
        return itemContents;
    }

    @Override
    public int getCount() {
        return itemContents.size();
    }

    @Override
    public Object getItem(int position) {
        return itemContents.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.repeat_item, null);
            holder = new Holder();
            holder.item = (ItemView)convertView.findViewById(R.id.item);
            holder.topLineLong = convertView.findViewById(R.id.top_line_long);
            holder.topLineShort = convertView.findViewById(R.id.top_line_short);
            holder.bottomLineLong = convertView.findViewById(R.id.bottom_line_long);
            holder.item.setArrowIcon(R.drawable.choosed_flag);
            convertView.setTag(holder);
        } else{
            holder = (Holder)convertView.getTag();
        }

        ItemContent itemContent = itemContents.get(position);

        setItemLine(holder, position);

        holder.item.setLeftTxt(itemContent.title);

        holder.item.showArrow(itemContent.selected);

        return convertView;
    }

    private void setItemLine(Holder holder, int position){

        if(position == 0){
            holder.topLineLong.setVisibility(View.VISIBLE);
            holder.topLineShort.setVisibility(View.GONE);
        } else{
            holder.topLineLong.setVisibility(View.GONE);
            holder.topLineShort.setVisibility(View.VISIBLE);
        }

        if(position == itemContents.size() - 1){
            holder.bottomLineLong.setVisibility(View.VISIBLE);
        } else{
            holder.bottomLineLong.setVisibility(View.GONE);

        }
    }

    class Holder{
        ItemView item;
        View topLineLong;
        View topLineShort;
        View bottomLineLong;
    }

    /**
     * 数据项
     */
    public static class ItemContent{
        public int repeat;
        public String title;
        public boolean selected;
    }
}
